# 集中化存储配置迁移演示脚本
Write-Host "===  存储配置集中化管理演示  ===" -ForegroundColor Green
Write-Host ""

# 项目路径
$projectPath = "d:\2025\JGSY.PP\Project\JGSY.CMS.LowCode.Platform"
Set-Location $projectPath

Write-Host "  配置集中化的优势:" -ForegroundColor Yellow
Write-Host "✅ 消除配置重复 - Upload和CloudStorage配置合并" -ForegroundColor Cyan
Write-Host "✅ 统一管理 - 单一配置入口Storage" -ForegroundColor Cyan  
Write-Host "✅ 向后兼容 - 保留现有服务正常工作" -ForegroundColor Cyan
Write-Host "✅ 功能增强 - 添加高级存储功能" -ForegroundColor Cyan
Write-Host "✅ 自动迁移 - 一键从旧配置迁移" -ForegroundColor Cyan

Write-Host ""
Write-Host "1. 检查当前配置状态:" -ForegroundColor Yellow

# 检查配置文件
if (Test-Path "appsettings.json") {
    $config = Get-Content "appsettings.json" | ConvertFrom-Json -ErrorAction SilentlyContinue
    if ($config) {
        Write-Host "当前配置结构:" -ForegroundColor White
        
        if ($config.Storage) {
            Write-Host "✅ Storage (集中化配置): 已存在" -ForegroundColor Green
            Write-Host "   - StorageType: $($config.Storage.StorageType)" -ForegroundColor Gray
            Write-Host "   - Local.RootPath: $($config.Storage.Local.RootPath)" -ForegroundColor Gray
        } else {
            Write-Host "❌ Storage (集中化配置): 不存在" -ForegroundColor Red
        }
        
        if ($config.Upload) {
            Write-Host "⚠️ Upload (旧配置): 存在 - 建议迁移" -ForegroundColor Yellow
            Write-Host "   - StorageType: $($config.Upload.StorageType)" -ForegroundColor Gray
            Write-Host "   - Local.RootPath: $($config.Upload.Local.RootPath)" -ForegroundColor Gray
        }
        
        if ($config.CloudStorage) {
            Write-Host "⚠️ CloudStorage (旧配置): 存在 - 建议迁移" -ForegroundColor Yellow  
            Write-Host "   - StorageType: $($config.CloudStorage.StorageType)" -ForegroundColor Gray
            Write-Host "   - Local.RootPath: $($config.CloudStorage.Local.RootPath)" -ForegroundColor Gray
        }
    }
}

Write-Host ""
Write-Host "2. 集中化配置优势对比:" -ForegroundColor Yellow

Write-Host "分散配置问题:" -ForegroundColor Red
Write-Host "  - Upload配置 + CloudStorage配置 = 重复维护" -ForegroundColor White
Write-Host "  - 配置不一致风险" -ForegroundColor White
Write-Host "  - 管理复杂度高" -ForegroundColor White

Write-Host "集中化配置优势:" -ForegroundColor Green  
Write-Host "  - Storage配置 = 统一管理入口" -ForegroundColor White
Write-Host "  - 自动配置一致性" -ForegroundColor White
Write-Host "  - 功能模块化组织" -ForegroundColor White
Write-Host "  - 易于扩展和维护" -ForegroundColor White

Write-Host ""
Write-Host "3. 集中化配置结构预览:" -ForegroundColor Yellow
Write-Host @"
Storage: {
  "StorageType": "Local",
  "MaxFileSize": 52428800,
  "Local": {
    "RootPath": "wwwroot/uploads",
    "BaseUrl": "/uploads"
  },
  "Thumbnail": {
    "Enabled": true,
    "Width": 200,
    "Height": 200
  },
  "Validation": {
    "StrictMimeTypeCheck": true,
    "BlockExecutableFiles": true  
  },
  "Features": {
    "EnableAutoBackup": false,
    "EnableVersionControl": false
  },
  "AliyunOSS": { ... },
  "TencentCOS": { ... },
  "HuaweiOBS": { ... }
}
"@ -ForegroundColor Cyan

Write-Host ""
Write-Host "4. 可用的API管理接口:" -ForegroundColor Yellow
Write-Host "配置分析:" -ForegroundColor Cyan
Write-Host "GET  /api/ConfigurationMigration/analyze                    - 分析迁移需求" -ForegroundColor White
Write-Host "GET  /api/ConfigurationMigration/centralized-config-status  - 集中化配置状态" -ForegroundColor White

Write-Host "配置迁移:" -ForegroundColor Cyan  
Write-Host "POST /api/ConfigurationMigration/migrate/{fileName}         - 迁移单个文件" -ForegroundColor White
Write-Host "POST /api/ConfigurationMigration/migrate-all                - 批量迁移所有文件" -ForegroundColor White

Write-Host "迁移验证:" -ForegroundColor Cyan
Write-Host "GET  /api/ConfigurationMigration/validate-migration         - 验证迁移结果" -ForegroundColor White
Write-Host "POST /api/ConfigurationMigration/rollback/{fileName}        - 回滚迁移" -ForegroundColor White

Write-Host "存储配置管理:" -ForegroundColor Cyan
Write-Host "GET  /api/StorageConfiguration/validate                     - 验证配置一致性" -ForegroundColor White
Write-Host "GET  /api/StorageConfiguration/unified                      - 获取统一配置" -ForegroundColor White

Write-Host ""
Write-Host "5. 编译检查:" -ForegroundColor Yellow
Write-Host "正在编译项目..." -ForegroundColor White

dotnet build --configuration Release --verbosity minimal
if ($LASTEXITCODE -eq 0) {
    Write-Host "✅ 项目编译成功 - 集中化配置功能就绪" -ForegroundColor Green
} else {
    Write-Host "❌ 项目编译失败 - 请检查代码" -ForegroundColor Red
}

Write-Host ""
Write-Host "6. 迁移建议:" -ForegroundColor Yellow
Write-Host "自动迁移步骤:" -ForegroundColor Cyan
Write-Host "1. 启动应用: dotnet run" -ForegroundColor White  
Write-Host "2. 分析配置: GET /api/ConfigurationMigration/analyze" -ForegroundColor White
Write-Host "3. 执行迁移: POST /api/ConfigurationMigration/migrate-all" -ForegroundColor White
Write-Host "4. 验证结果: GET /api/ConfigurationMigration/validate-migration" -ForegroundColor White

Write-Host ""
Write-Host "手动迁移步骤:" -ForegroundColor Cyan
Write-Host "1. 备份现有配置文件" -ForegroundColor White
Write-Host "2. 复制 appsettings-centralized.json 为模板" -ForegroundColor White
Write-Host "3. 将现有Upload和CloudStorage配置合并到Storage配置" -ForegroundColor White
Write-Host "4. 移除或标记废弃旧配置" -ForegroundColor White

Write-Host ""
Write-Host "是否查看集中化配置模板? (Y/N): " -NoNewline -ForegroundColor Yellow
$response = Read-Host

if ($response -eq "Y" -or $response -eq "y") {
    if (Test-Path "appsettings-centralized.json") {
        Write-Host ""
        Write-Host "=== 集中化配置模板内容 ===" -ForegroundColor Green
        Write-Host ""
        Get-Content "appsettings-centralized.json" | Select-Object -First 50
        Write-Host ""
        Write-Host "... (配置模板已显示前50行)" -ForegroundColor Gray
        Write-Host "完整模板请查看: appsettings-centralized.json" -ForegroundColor Cyan
    } else {
        Write-Host "配置模板文件不存在" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "是否启动应用进行实际测试? (Y/N): " -NoNewline -ForegroundColor Yellow
$startResponse = Read-Host

if ($startResponse -eq "Y" -or $startResponse -eq "y") {
    Write-Host ""
    Write-Host "启动应用..." -ForegroundColor Green
    Write-Host "访问 https://localhost:7001/swagger 查看配置管理API" -ForegroundColor Cyan
    Write-Host "使用配置迁移API实现存储配置的集中化管理" -ForegroundColor Cyan
    Write-Host ""
    dotnet run --configuration Release
} else {
    Write-Host ""
    Write-Host "  集中化存储配置管理演示完成!" -ForegroundColor Green
    Write-Host "优势总结:" -ForegroundColor Yellow
    Write-Host "• 消除配置重复和不一致问题" -ForegroundColor White
    Write-Host "• 提供统一的配置管理入口" -ForegroundColor White  
    Write-Host "• 支持高级存储功能" -ForegroundColor White
    Write-Host "• 自动迁移和验证工具" -ForegroundColor White
    Write-Host "• 完整的API管理界面" -ForegroundColor White
}
